package model

import "time"

type CozeConversation struct {
	Id             int       `json:"id" gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	UserId         int       `json:"user_id" gorm:"type:int(11); COMMENT:'用户id'"`
	ConversationId string    `json:"conversation_id" gorm:"type:varchar(255); COMMENT:'会话id'"`
	Title          string    `json:"title" gorm:"type:varchar(255); COMMENT:'会话标题'"`
	MetaData       string    `json:"meta_data" gorm:"type:text; COMMENT:'创建消息时的附加消息，获取消息时也会返回此附加消息。'"`
	Status         int       `json:"status" gorm:"type:int(11); COMMENT:'会话状态'"`
	CreatedAt      time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt      time.Time `json:"updated_at" gorm:"type:datetime"`
}

const (
	CozeConversationStatusEnd    = 0
	CozeConversationStatusActive = 1
)

func (c *CozeConversation) TableName() string {
	return "coze_conversation"
}

// GetConversation 获取会话
func (c *CozeConversation) GetConversation(conversationId string, userId int) (CozeConversation, error) {

	db := GetInstance().GetDB()
	var conversation CozeConversation
	if err := db.Model(&CozeConversation{}).
		Where("conversation_id = ?", conversationId).
		Where("user_id = ?", userId).
		Where("status = ?", CozeConversationStatusActive).
		First(&conversation).Error; err != nil {
		return conversation, err
	}
	return conversation, nil
}
